package service;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import service.java.Convierte;

public class Formato extends Convierte {

    public String aSoles(String valor) {
        String result = null;
        Double x = aReal(valor);

        if (x != null) {
            NumberFormat numberFormat =
                    NumberFormat.getNumberInstance(Locale.UK);
            DecimalFormat decimalFormat = (DecimalFormat) numberFormat;
            decimalFormat.applyPattern("###,###,###,###.00");

            result = "S/. " + decimalFormat.format(x);
        }

        return result;
    }

    public String nDecimales(String valor, int decimales) {
        String result = null;
        Double x = aReal(valor);

        if (x != null) {
            String deci = ".";
            for (int i = 0; i < decimales; i++) {
                deci += "0";
            }

            NumberFormat numberFormat =
                    NumberFormat.getNumberInstance(Locale.UK);
            DecimalFormat decimalFormat = (DecimalFormat) numberFormat;

            deci = (deci.compareTo(".") == 0)
                    ? "###,###,###,###" : "###,###,###,###" + deci;
            decimalFormat.applyPattern(deci);

            result = decimalFormat.format(x);
        }

        return result;
    }

    public Formato() {
    }
}
